﻿using System;
using System.Collections.Generic;
using ZyGames.DZPocker.Model;
using ZyGames.Framework.Cache.Generic;
using ZyGames.Framework.Game.Cache;
using ZyGames.Framework.Game.Context;
using ZyGames.Framework.Game.Contract;
using ZyGames.Framework.Game.Contract.Action;
using ZyGames.Framework.Game.Lang;
using ZyGames.Framework.Game.Service;
using System.Linq;
using ZyGames.DZPocker.Bll.Logic;
using ZyGames.DZPocker.Script;
using System.Text.RegularExpressions;
using ZyGames.Framework.Net;
using ZyGames.Framework.Common.Serialization;
using ZyGames.DZPocker.Tools;

namespace ZyGames.DZPocker.Script.CsScript.Action
{
	/// <summary>
	/// 客户端申请显示游客列表和实时战绩
	/// </summary>
	public class Action30070 : BaseAction
	{
		private int tableId;
		private int userId;

		private TableData _currentTable;
		private GameUser _currentGameUser;
		private bool result = false;

		public Action30070(HttpGet actionGetter)
			: base((short)ActionType.DZApplyShowRealTimeRecordAndTourists, actionGetter)
		{
		}
		public override bool GetUrlElement()
		{
			if (httpGet.GetInt("TableId", ref tableId) &&
				httpGet.GetInt("UserId", ref userId))
			{
				return true;
			}
			return false;
		}

		public override bool TakeAction()
		{
			_currentTable = GameRoom.Current.GetTableData(tableId);
			if (_currentTable == null)
			{
				Tool.WriteWarmingLine("消息30070获得房间不存在");
				return false;
			}
			_currentGameUser = GameTable.Current.GetUser(userId);
			if (_currentGameUser == null)
			{
				Tool.WriteWarmingLine("消息30050玩家不存在");
				return false;
			}
			this.result = true;
			return true;
		}

		public override void TakeActionAffter(bool state)
		{
			if (result)
			{
				//搜集游客玩家以及实时战绩
				var allPlayers = ClientNotifier.GetUserList(_currentTable);
				var touristsList = new List<GameUser>();
				//选择没有坐下的玩家
				foreach (var player in allPlayers)
				{
					if (player.Property.GetPositionId(tableId) == 0)
						touristsList.Add(player);
				}

				//实时战绩,要带上保险人
				var realTimeRecordList = new List<RealTimeRecord>();
				foreach (var record in _currentTable.GetRealTimeRecordList())
				{
					var realRecord = record.Value;
					var newRecord = new RealTimeRecord();
					newRecord.SUser = realRecord.SUser;
					newRecord.BuyChip = realRecord.BuyChip;
					//判断当前是否在位子上，如果在的话则要加上当前自己位置上的筹码
					var posChip = 0;
					foreach (var posData in _currentTable.Positions)
					{
						if (posData.UserId == newRecord.SUser.UserId)
						{
							posChip = posData.Chip;
							break;
						}
					}
					newRecord.WinChip = posChip + realRecord.LeftChip - realRecord.BuyChip;  //当前剩余的筹码-申请的筹码数量
					realTimeRecordList.Add(newRecord);
				}
				//开通保险才添加保险人
				if (_currentTable.Insurance)
				{
					var insurer = GameTable.Current.GetUser(_currentTable.InsureId);
					if (insurer != null)
					{
						var insurerRecord = new RealTimeRecord() { BuyChip = 0, SUser = new SimpleUserData() { UserId = insurer.UserId, NickName = insurer.GetNickName(), HeadIcon = insurer.HeadIcon }, WinChip = _currentTable.TableInsurer.Chip, IsInsurancer = true};
						realTimeRecordList.Add(insurerRecord);
					}
				}

				var parameters = new Parameters();
				parameters["TableID"] = tableId;
				parameters["LeftTime"] = _currentTable.GetLeftTime();
				parameters["Hand"] = _currentTable.HandVersionID;
				parameters["Tourists"] = Convert.ToBase64String(ProtoBufUtils.Serialize(touristsList));
				parameters["RealTimeRecords"] = Convert.ToBase64String(ProtoBufUtils.Serialize(realTimeRecordList));
				GameTable.Current.SyncNotifyAction((int)ActionType.DZShowRealTimeRecordAndTourists, _currentGameUser, parameters, t =>
				{
					Console.WriteLine("给某个人下发Action 30071 实时战绩信息");
				});
			}
			base.TakeActionAffter(state);
		}
	}
}
